package servlet;

import pojo.Dept;
import service.DeptService;
import service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;

import static com.mysql.cj.conf.PropertyKey.logger;

@WebServlet(urlPatterns = "/dept")
public class DeptServlet extends HttpServlet {
    DeptService deptService = new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DeptService deptService = new DeptServiceImpl();
        req.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if("list".equals(action)){
            String name = req.getParameter("name");
            String position= req.getParameter("position");

            Dept condtion= new Dept(name,position);

            List<Dept> depts = deptService.findAll(condtion);
            req.setAttribute("depts", depts);
            // 转发
            req.getRequestDispatcher("/list.jsp").forward(req, resp);
        }
        else if ("add".equals(action)) {
            try {
                String idStr = req.getParameter("id");
                if (idStr == null || idStr.trim().isEmpty()) {
                    throw new IllegalArgumentException("ID 不能为空，请输入有效的数字");
                }
                int id = Integer.parseInt(idStr);
                String name = req.getParameter("name");
                String position = req.getParameter("position");

                System.out.println("Adding department with ID: " + id + ", Name: " + name + ", Position: " + position);

                int result = deptService.addDept(new Dept(id, name, position));
                if (result > 0) {
                    resp.sendRedirect(req.getContextPath() + "/dept?action=list");
                } else {
                    resp.sendRedirect(req.getContextPath() + "/dept?action=add&error=添加部门失败");
                }
            } catch (NumberFormatException e) {
                logger.name();
                resp.sendRedirect(req.getContextPath() + "/dept?action=add&error=ID 格式不正确，请输入有效的数字");
            } catch (IllegalArgumentException e) {
                logger.name();
                resp.sendRedirect(req.getContextPath() + "/dept?action=add&error=" + e.getMessage());
            } catch (Exception e) {
                logger.name();
                resp.sendRedirect(req.getContextPath() + "/dept?action=add&error=添加部门时发生错误");
            }
        }
        else if("delete".equals(action)){
            String sid = req.getParameter("id");
            Integer id= Integer.parseInt(sid);
            //根据id查询用户信息
            int result=deptService.deleteDeptById(id);
            if(result>0)
                req.getRequestDispatcher("/dept?action=list").forward(req,resp);
            else
                resp.sendRedirect("/error.jsp");
        }
        else if("update".equals(action)){
            String sid=req.getParameter("id");
            int id=Integer.parseInt(sid);
            //拿到页面传递的参数
            String name = req.getParameter("name");
            String position = req.getParameter("position");
            int result = deptService.updateDept(new Dept(id,name, position));
            if(result>0) {
                resp.sendRedirect("dept?action=list");
            }
            else
                resp.sendRedirect("/error.jsp");
        }
        else if("toupdate".equals(action)){
            String sid = req.getParameter("id");
            Integer id= Integer.parseInt(sid);
            //根据id查询用户信息
            Dept dept=deptService.findById(id);
            //将对象存储在作用域中
            req.setAttribute("dept",dept);
            //转发到修改页面
            req.getRequestDispatcher("update.jsp").forward(req,resp);
        }
    }
}